package com.group4.Servlets;

import com.group4.Utility.UserValidator;
import com.group4.DAO.*;
import com.group4.DB.User;
import com.group4.DB.Video;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.group4.Utility.UserValidator;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 102831973239L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("username");
		if(username != null){
			//HttpSession session = request.getSession();
			List<VideoDAO> videoList = Video.getVideos(User.getUserWithUsername(username).getID());
			//List<String> videos = new ArrayList();
			//List<String> links = new ArrayList();
			Map<String, String> videos = new HashMap<String, String>();
			for(int i=0;i<videoList.size();i++){
				//System.out.println("ininininin");
				//videos.add(videoList.get(i).getVideoName());
				//links.add(videoList.get(i).getVideoUrl());
				videos.put(videoList.get(i).getVideoUrl(), videoList.get(i).getVideoName());
			}
			session.setAttribute("videos", videos);
			//session.setAttribute("links", links);
			session.setAttribute("username", username);
			RequestDispatcher rd = request.getRequestDispatcher("/user/profile.jsp");
			rd.forward(request, response);
		}else{
			RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
			rd.forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		if(UserValidator.validateUser(username, password)){
			HttpSession session = request.getSession();
			List<VideoDAO> videoList = Video.getVideos(User.getUserWithUsername(username).getID());
			//List<String> videos = new ArrayList();
			//List<String> links = new ArrayList();
			Map<String, String> videos = new HashMap<String, String>();
			for(int i=0;i<videoList.size();i++){
				//System.out.println("ininininin");
				//videos.add(videoList.get(i).getVideoName());
				//links.add(videoList.get(i).getVideoUrl());
				videos.put(videoList.get(i).getVideoUrl(), videoList.get(i).getVideoName());
			}
			session.setAttribute("videos", videos);
			//session.setAttribute("links", links);
			session.setAttribute("username", username);
			//Setting session expiration time
			session.setMaxInactiveInterval(30*60);
			Cookie cookie = new Cookie("user", username);
			//Setting cookie expiration time
			cookie.setMaxAge(30*60);
			response.addCookie(cookie);
			RequestDispatcher rd = request.getRequestDispatcher("/user/profile.jsp");
			rd.forward(request, response);
		}
		else{
			request.setAttribute("loggedIn", "false");
			RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
			rd.forward(request, response);
		}
	}
}
